package com.gift.controller;

import com.gift.common.ExcelUtil;
import com.gift.common.ExcelUtil2;
import com.gift.common.TimeHandler;
import com.gift.service.ReportService;
import com.gift.vo.Table201VO;
import com.gift.vo.Table3VO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Controller
@RequestMapping("report")
public class ReportController {
    @Resource
    ReportService reportService;


    @RequestMapping(value = "toReport")
    public String toReport(){

        return "ReportController/analysis";
    }


    @RequestMapping(value = "Export01")
    @ResponseBody
    public void partExportHttpServletResponse (HttpServletRequest request, HttpServletResponse response, @RequestParam Map<String,String> map)  {

        Map<String,Object> mapO=TimeHandler.findParmHandler(map);

        List jj =reportService.getReportVOList1(mapO);

        //获取属性-列头
        Map<String,String> headMap =  new LinkedHashMap<String,String>();
        headMap.put("time","时间");
        headMap.put("bankCard","收钱户头");
        headMap.put("ganmeName","对应的游戏名");
        headMap.put("getMoney","卖分得钱");
        headMap.put("inAllGetMoneyAcc","所占得钱中比例");
        headMap.put("sendMoney","买分支出");
        headMap.put("poundage","手续费");
        headMap.put("inAllSendMoneyAcc","所占出钱中比例");

        String time = mapO.get("ssbegin")+"至"+mapO.get("ssend");

        String title = time+"随交易银行流水报表";
        title = title.replace("T"," ");

        ExcelUtil.downloadExcelFile(title, headMap, jj, response);

    }


    @RequestMapping(value = "Export02")
    @ResponseBody
    public void partExportHttpServletResponse2 (HttpServletRequest request, HttpServletResponse response, @RequestParam Map<String,String> map)  {
        Map<String,Object> mapO=TimeHandler.findParmHandler(map);
        List<Table201VO> dataList1 =reportService.getReportVOList201(mapO);
        List<Table201VO> dataList2 =reportService.getReportVOList202(mapO);
        //获取属性-列头
        Map<String,String> headMap =  new LinkedHashMap<String,String>();
        headMap.put("time","时间");
        headMap.put("zaoU","早班人");
        headMap.put("zhongU","中班人");
        headMap.put("wanU","晚班人");
        headMap.put("getMoney","总收入");
        headMap.put("sendMoney","总支出");

        //获取属性-列头2
        Map<String,BigDecimal> totalMap =  new LinkedHashMap<String,BigDecimal>();
        BigDecimal getAllDMoney = BigDecimal.ZERO;
        BigDecimal sendAllDMoney = BigDecimal.ZERO;
        BigDecimal workRoom = BigDecimal.ZERO;
        BigDecimal sendAllMoney = BigDecimal.ZERO;
        for(Table201VO t201v:dataList1){
            getAllDMoney = getAllDMoney.add(t201v.getGetMoney());
            sendAllDMoney = sendAllDMoney.add(t201v.getSendMoney());
        }
        for(Table201VO t201v:dataList2){
            if("工作室支出".equals(t201v.getDelTpye())){
                workRoom.add(t201v.getSendMoney());
            }else{
                sendAllMoney.add(t201v.getSendMoney());
            }
        }
        totalMap.put("此段时间卖分收入",getAllDMoney);
        totalMap.put("此段时间买分支出",sendAllDMoney);
        totalMap.put("工作室支出",workRoom);
        totalMap.put("其他总支出",sendAllMoney);


        for(Table201VO t201v:dataList2){
            dataList1.add(t201v);
        }


        String time = mapO.get("ssbegin")+"至"+mapO.get("ssend");

        String title = time+"每日收入支出报表";
        title = title.replace("T"," ");

        ExcelUtil2.downloadExcelFile(title, headMap, dataList1,totalMap, response);
    }

    @RequestMapping(value = "Export03")
    @ResponseBody
    public void partExportHttpServletResponse3 (HttpServletRequest request, HttpServletResponse response, @RequestParam Map<String,String> map)  {
        Map<String,Object> mapO=TimeHandler.findParmHandler(map);
        List<Table3VO> dataList =reportService.getReportVOList3(mapO);
        //获取属性-列头
        Map<String,String> headMap =  new LinkedHashMap<String,String>();
        headMap.put("time","时间");
        headMap.put("shift","班次");
        headMap.put("handlers","本班上班人");
        headMap.put("remark","备注");
        headMap.put("gname","游戏名字");
        headMap.put("sellingscore","卖出多少分");
        headMap.put("giftscore","送出多少分");
        headMap.put("buyscore","买了多少分");
        headMap.put("proportion","分数和钱的比例");
        headMap.put("helpscore","帮忙敲分");
        headMap.put("helpedname","被帮助者的名字");

        String time = mapO.get("ssbegin")+"至"+mapO.get("ssend");

        String title = time+"分数流水报表";
        title = title.replace("T"," ");

        ExcelUtil.downloadExcelFile(title, headMap, dataList, response);
    }


}
